package com.example.hzq.food.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hzq.food.R;
import com.example.hzq.food.bean.BuyBean;
import com.example.hzq.food.bean.CollectBean;
import com.example.hzq.food.bean.FoodBean;
import com.example.hzq.food.model.Model;
import com.squareup.picasso.Picasso;


import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.hzq.food.activity.Login.userid;

/**
 * Created by hzq on 2017/6/12.
 */

public class ShopFoodadapter extends RecyclerView.Adapter{
    private  int foodnum;
    private  double foodsumprice;
    private List<FoodBean>readybuy=new ArrayList<>();
    private List<Integer> count=new ArrayList<>();
    public BuyBean buyBean;
    private List<FoodBean> list;
    private Context context;
    private LayoutInflater layoutInflater;
    private  boolean foodcollect;
    public ShopFoodadapter(Context context){
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    public void setList(List<FoodBean>list){
        this.list=list;
        notifyDataSetChanged();
    }

    private void changeShopCart() {
        EventBus.getDefault().post(new BuyBean(buyBean));

    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView tv_name,tv_price,tv_num;
        private ImageButton ib_add,ib_sub,ib_collect;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.imageView4);
            tv_name = (TextView) itemView.findViewById(R.id.textView30);
            tv_price = (TextView) itemView.findViewById(R.id.textView31);
            ib_add=(ImageButton)itemView.findViewById(R.id.imagebutton8);
            ib_sub=(ImageButton)itemView.findViewById(R.id.imagebutton7);
            ib_collect=(ImageButton)itemView.findViewById(R.id.imagebutton9);
            tv_num=(TextView)itemView.findViewById(R.id.textview32);
        }
}
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=layoutInflater.inflate(R.layout.item2,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder,final int position) {
        final FoodBean foodBean=list.get(position);
        if (foodBean == null)
            return;
        final ViewHolder viewHolder=(ViewHolder)holder;
        if (!foodBean.getPic().equals(""))
            Picasso.with(context).load(foodBean.getPic()).placeholder(R.drawable.ic_default_adimage).into(viewHolder.imageView);
        else viewHolder.imageView.setImageResource(R.drawable.ic_default_adimage);
        final int id=Integer.valueOf(userid);
        Model model=new Model();
        Call<CollectBean>call=model.iscollect(id,foodBean.getFood_id(),1);
        call.enqueue(new Callback<CollectBean>() {
            @Override
            public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                if (response.isSuccessful()&&response!=null){
                    if (response.body().getCollected().toString().equals("1")){
                        viewHolder.ib_collect.setImageResource(R.drawable.ic_collect1);
                           foodcollect=true;
                    }
                   else {
                        viewHolder.ib_collect.setImageResource(R.drawable.ic_collect);
                        foodcollect=false;
                    }
                }
            }
            @Override
            public void onFailure(Call<CollectBean> call, Throwable t) {

            }
        });
        viewHolder.tv_name.setText(foodBean.getFoodname());
        viewHolder.tv_price.setText(foodBean.getPrice()+"");
        viewHolder.ib_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Model model=new Model();
                Call<CollectBean>call=model.collectornofood(id,foodBean.getFood_id());
                call.enqueue(new Callback<CollectBean>() {
                    @Override
                    public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                        if (response.isSuccessful()&&response!=null){
                            if (foodcollect&&response.body().getSuccess().toString().equals("1")){
                                foodcollect=false;
                                viewHolder.ib_collect.setImageResource(R.drawable.ic_collect);
                            }
                            else if(foodcollect==false&&response.body().getSuccess().toString().equals("1")){
                                foodcollect=true;
                                viewHolder.ib_collect.setImageResource(R.drawable.ic_collect1);
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<CollectBean> call, Throwable t) {

                    }
                });
            }
        });
        viewHolder.ib_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Integer.parseInt(viewHolder.tv_num.getText().toString())==0) {
                    viewHolder.ib_sub.setVisibility(View.VISIBLE);
                    viewHolder.tv_num.setVisibility(View.VISIBLE);
                }
                viewHolder.tv_num.setText((Integer.parseInt(viewHolder.tv_num.getText().toString())+1)+"");
                foodnum++;
                foodsumprice+=foodBean.getPrice();
                if(buyBean!=null){
                    boolean flag=true;
                    for(int i=0;i<buyBean.getFood().size();i++){
                        if(buyBean.getFood().get(i).getFood_id()==foodBean.getFood_id()){
                            buyBean.getCount().set(i,buyBean.getCount().get(i)+1);
                            buyBean.setFoodnum(foodnum);
                            buyBean.setFoodsum(foodsumprice);
                            flag=false;
                        }
                    }
                    if(flag){
                        buyBean.getFood().add(foodBean);
                        buyBean.getCount().add(1);
                        buyBean.setFoodnum(foodnum);
                        buyBean.setFoodsum(foodsumprice);
                    }
                }else{
                    readybuy.add(foodBean);
                    count.add(1);
                    buyBean=new BuyBean(foodnum,foodsumprice,readybuy,count);
                }
                changeShopCart();
            }

        });
        viewHolder.ib_sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Integer.parseInt(viewHolder.tv_num.getText().toString()) > 0){
                    viewHolder.tv_num.setText((Integer.parseInt(viewHolder.tv_num.getText().toString())-1)+"");
                  //  Toast.makeText(context,"sub",Toast.LENGTH_SHORT).show();
                    foodnum--;
                    foodsumprice-=foodBean.getPrice();
                   for(int i=0;i<buyBean.getFood().size();i++){
                        if(buyBean.getFood().get(i).getFood_id()==foodBean.getFood_id()){
                            buyBean.getCount().set(i,buyBean.getCount().get(i)-1);
                            buyBean.setFoodnum(foodnum);
                            buyBean.setFoodsum(foodsumprice);
                            if(buyBean.getCount().get(i)==0){
                                buyBean.getCount().remove(i);
                                buyBean.getFood().remove(i);
                            }
                        }
                    }

                }
                if(Integer.parseInt(viewHolder.tv_num.getText().toString())==0) {
                    viewHolder.ib_sub.setVisibility(View.INVISIBLE);
                    viewHolder.tv_num.setVisibility(View.INVISIBLE);
                }
                changeShopCart();
            }
        });
    }
    @Override
    public int getItemCount() {
        if(list==null) return  0;
        else
            return list.size();
    }

}
